<?php
/* $Id: bufs.issues.php Xavier VILAIN  $ */
/**
 * @package Issue-Tracker
 * @subpackage Issues
 */

	// Selection Screen
	$o_ecran = new Ecran('E1');
		$o_block = new Block('B1','Search Events','?module=issues&action=bugs&butok=true');
			// Periode
			$o_block->Add(new SelOpt('logged',true));
			// Bouton Valider
		$o_block->Add(new GButton('butok'));
	$o_ecran->Add($o_block);
	
	if(isset($_POST['butok']))
	{
		$wqh = $o_ecran->GetWhere();
		if(!errors())
		{
			global $dbi;
			// Select All Event:Email for now
			$sql  = "SELECT ilogid,issueid,userid,logged,message,msg_var,private,z_type ";
			$sql .= "FROM issue_log ";
			$sql .= "WHERE message = 'notified' ";
			if(!empty($wqh ))
				$sql .= " $wqh ";
			$sql .= "ORDER BY issueid,logged ";
			$t_data = $dbi->fetch_all($sql,'array');
			if(is_array($t_data))
			{
				foreach($t_data as $key => $s_data)
				{
					$iid = $s_data['issueid'];
					if(!isset($t_gid[$iid]))
					{
						$o_issue = new Issue($iid);
						$t_gid[$iid]['gid'] = $o_issue->gid;
						$t_list = $o_issue->GetReceivers();
						if(is_array($t_list))
						{
							$t_gid[$iid]['notif'] = '';
							foreach($t_list as $usrid)
							{
								if($usrid == $_SESSION['userid'])
									continue;
								$user  = new User($usrid);
								$t_email = $user->GetGrpEmail($o_issue->gid);
								foreach($t_email as $email)
								{
									$t_gid[$iid]['notif'] .=  ",".$email;
									$t_notif[$iid][$email] = $email;
								}
							}
							$t_gid[$iid]['notif'] = substr($t_gid[$iid]['notif'],1);
							if($s_data['msg_var'] != $t_gid[$iid]['notif'])
							{
								$t_notes = explode(",",$s_data['msg_var']);
								foreach($t_notes as $email)
								{
									unset($t_notif[$iid][$email]);
								}
								$t_gid[$iid]['notif'] = implode(",",$t_notif[$iid]);
							}
							else
								$t_gid[$iid]['notif'] = '';
						}
						
					}
					$s_edit['gid'] = $t_gid[$iid]['gid'];
					$s_edit['issueid'] = $iid;
					$s_edit['logged'] = $s_data['logged'];
					$s_edit['email'] = $t_gid[$iid]['notif'];
					// Should have been
					if(!empty($s_edit['email']))
						$t_edit[] = $s_edit;
				}
			}
			$o_grid = new Grid();
			$o_grid->SetFieldcat(array('gid','issueid','logged','email'));
			$o_grid->setFieldAttr('logged',"DisplayTime");
			$o_grid->SetDatas($t_edit);
		}
	}
	echo $o_ecran->Display();
	if(isset($o_grid))
		echo $o_grid->Display();
?>
